//
// $Id$
//

#include<__vic/logger.h>
#include<cstring>

namespace __vic {

//----------------------------------------------------------------------------
logger::logger(output &out, severity_t level)
    : out(&out), rec_objs_count(0), log_level(level)
{
    cur_msg.reserve(min_buffer_size);
}
//----------------------------------------------------------------------------
logger::logger(settings_t s)
    : out(&s.output()), rec_objs_count(0), log_level(s.level())
{
    cur_msg.reserve(min_buffer_size);
}
//----------------------------------------------------------------------------
logger::~logger()
{
}
//----------------------------------------------------------------------------
void logger::message(severity_t s, const char *msg, size_t msg_len)
{
    if(s >= level()) out->publish_record(s, msg, msg_len);
}
//----------------------------------------------------------------------------
#if !__cpp_lib_string_view
void logger::message(severity_t s, const char *msg)
{
    if(s >= level()) out->publish_record(s, msg, std::strlen(msg));
}
#endif
//----------------------------------------------------------------------------
void logger::flush_()
{
    // Micro-optimization: Avoiding message() fuction call
    //message(cur_severity, cur_msg.data(), cur_msg.length());
    if(cur_severity >= level())
        out->publish_record(cur_severity, cur_msg.data(), cur_msg.length());
    cur_msg.clear();
    //shrink_buffer(hi_water_mark); // prevent uncontrolled buffer growth
}
//----------------------------------------------------------------------------
#if __cpp_lib_format >= 202106L
void logger::vformat_(
    severity_t s, std::string_view fmt, std::format_args args)
{
    std::vformat_to(
        std::back_insert_iterator<std::string>{cur_msg}, fmt, args);
    out->publish_record(s, cur_msg.data(), cur_msg.length());
    cur_msg.clear();
}
#endif
//----------------------------------------------------------------------------

} // namespace
